<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller {
    public function goods(){
    	$goods=D('Goods');
    	$count = $goods->count();
        $Page       = new \Think\Page($count,5);//继承
        $show       = $Page->show();//展示
        $list = $goods->limit($Page->firstRow.','.$Page->listRows)->select();
        $lists = $goods->select();
       
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->assign('lists',$lists);
       
    	$this->display(); 
    }

    public function goodsadd(){

           $goods=D('Goods');
           if(IS_POST){
             
            
    		$data['goods_name']=I('goods_name');
    		$data['goods_price']=I('goods_price');
    		$data['intro']=I('intro');
 	
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './Upload/'; // 设置附件上传根目录
            $upload->savePath  =     ''; // 设置附件上传（子）目录
            // 上传文件
            $info   =   $upload->upload();                                                                                                                                                        
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{// 上传成功
                $img_path1 = './Upload/'.$info['goods_img']['savepath'];//图片的目录
                $img_path2 = $info['goods_img']['savename'];//图片的名字

                $image = new \Think\Image();
                $image->open($img_path1.$img_path2);
                // 按照原图的比例生成一个最大为120*90的缩略图并保存为thumb.jpg

                $img_xiao = './Upload/thumb/'.$img_path2;
                $image->thumb(120, 90)->save($img_xiao);
                $data['goods_small_img'] = $img_xiao;
                $data['goods_img'] = $img_path1.$img_path2;
            }
            //var_dump($data);exit;

            if(!$goods->create($data)){
                echo $goods->getError();
                exit;
            }else
            {	
                if($goods->add())
                {
                   $this->success('新增成功！',U('/admin/goods/goods',3));
                    
                }else
                {
                    $this->error('新增失败！');
                }

            }
        }
        
        $this->display();
    



    }
}